package com.ming.closeto.adapter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ming.closeto.R;
import com.ming.closeto.model.FileBrowseModel;

import java.util.List;

public class FileBrowseAdapter extends RecyclerView.Adapter<FileBrowseAdapter.ViewHolder> {

    private Context mContext;
    private LayoutInflater inflater;
    private List<FileBrowseModel> mList;

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public FileBrowseAdapter(Context mContext, List<FileBrowseModel> mList) {
        this.mContext = mContext;
        this.mList = mList;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.layout_file_br_item, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
        FileBrowseModel model = mList.get(position);
        viewHolder.iv_icon.setBackgroundResource(model.getIcon());
        viewHolder.tv_title.setText(model.getTitle());
        viewHolder.tv_path.setText(model.getPath());

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClick(position);
            }
        });

        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                onItemClickListener.onLongClick(position);
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView iv_icon;
        private TextView tv_title;
        private TextView tv_path;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_path = itemView.findViewById(R.id.tv_path);
        }
    }

    public interface OnItemClickListener {
        // 点击事件
        void onClick(int position);
        // 长安事件
        void onLongClick(int position);
    }
}
